﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class shooter : MonoBehaviour {

	public float sleepTime = 1.0f;
	public float moveSpeedToB=100.0f;
	public GameObject bullet;
	// Use this for initialization
	Vector3 apoint;
	Vector3 bpoint;
	void Start () {
		apoint = this.transform.localPosition;
		var pp =  this.GetComponentInChildren<point> ().transform;
		bpoint = this.transform.parent.worldToLocalMatrix.MultiplyPoint( pp.position);
		pp.gameObject.SetActive (false);
	}

	float timer=0;
	// Update is called once per frame
	void Update () {
		timer += Time.deltaTime;
		if (timer > sleepTime) {
			timer = 0;
			var nobj = GameObject.Instantiate (bullet);
			nobj.transform.parent = this.transform;
			nobj.transform.localPosition = apoint;
		}
		foreach (Transform c in this.transform) {
		
			var dir = bpoint - c.localPosition;
			var len = dir.magnitude;
			if (len < 0.001) {
				GameObject.Destroy (c.gameObject);
				continue;
			}
			var add = moveSpeedToB * Time.deltaTime;
			if (add > len)
				add = len;
			c.localPosition = c.localPosition + add * dir.normalized;
		}
	}
}
